cmake_minimum_required(VERSION 3.27)
project(Raft VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 查找依赖库
find_package(GTest CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
find_package(async_simple CONFIG REQUIRED)

# BqLog库
set(LIB_BQ_LOG_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib/bq_log)
find_library(LIB_BQ_LOG NAMES BqLog PATHS ${LIB_BQ_LOG_PATHS} NO_DEFAULT_PATH)

# 设置源文件
set(RAFT_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_utils/Config.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_utils/DataStream.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_utils/Persistence.cpp

    ${CMAKE_CURRENT_SOURCE_DIR}/raft_middleware/rpc_proto/middleware_rpc.pb.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_middleware/rpc_proto/middleware_rpc.grpc.pb.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_middleware/RaftMiddleware.cpp

    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/rpc_proto/raft_rpc.pb.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/rpc_proto/raft_rpc.grpc.pb.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/ApplyMsg.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/RaftSender.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/RaftCore.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/raft_core/RaftCoreGrpc.cpp
)

# 创建动态库
add_library(Raft SHARED ${RAFT_SOURCES})
set_target_properties(Raft PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
)

# 设置包含目录
target_include_directories(Raft PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/bq_log/include
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/rapid_json/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(Raft PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)


# 链接库
target_link_libraries(Raft PUBLIC
    ${LIB_BQ_LOG}
    GTest::gtest
    protobuf::libprotobuf
    gRPC::grpc++
    async_simple::async_simple
)


# # 安装目标（可选，如果需要将库安装到系统或指定目录）
# install(TARGETS Raft
#     LIBRARY DESTINATION lib
#     ARCHIVE DESTINATION lib
#     RUNTIME DESTINATION bin
#     PUBLIC_HEADER DESTINATION include/raft
# )

# # 安装头文件（可选，如果需要将头文件暴露给外层）
# install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/raft_untils
#               ${CMAKE_CURRENT_SOURCE_DIR}/include/state_machine
#               ${CMAKE_CURRENT_SOURCE_DIR}/include/raft_middleware
#               ${CMAKE_CURRENT_SOURCE_DIR}/include/raft_core
#         DESTINATION include/raft
#         FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
# )